package org.objective.base;

import org.common.service.ServiceRegister;
import org.objective.command.AuthToken;
import org.objective.command.Command;

import org.objective.dal.commands.BaseObjectSequenceCommand;
import org.objective.dal.commands.BaseTypeSequenceCommand;

/**
 *
 * @author gus
 */
public class SequenceIDGenerator
{
    public long nextID(Class type)
    {
        Command<Long> command = getCommand(type);
        if (command != null)
        {
            try
            {
                return command.execute(ServiceRegister.getInstance(), new AuthToken()).getValue();
            }
            catch (Exception ex)
            {
            }
        }
        return -1;
    }

    private Command<Long> getCommand(Class type)
    {
        Command<Long> command = null;
        if (BaseType.class.isAssignableFrom(type))
        {
            command = new BaseTypeSequenceCommand();
        }
        else if (BaseObject.class.isAssignableFrom(type))
        {
            command  = new BaseObjectSequenceCommand();
        }

        return command;
    }
}
